package hu.blok1.veilingsite.client;

import hu.blok1.veilingsite.client.gui.CreateProductListGUI;
import hu.blok1.veilingsite.client.gui.ProductSpecificationsGUI;
import hu.blok1.veilingsite.client.gui.UserCreateProductGUI;
import hu.blok1.veilingsite.client.gui.adminIndex;
import hu.blok1.veilingsite.client.gui.adminProductStatus;
import hu.blok1.veilingsite.client.gui.adminUserStatus;
import hu.blok1.veilingsite.client.gui.createAccountGUI;
import hu.blok1.veilingsite.client.gui.footerGUI;
import hu.blok1.veilingsite.client.gui.headerGUI;
import hu.blok1.veilingsite.client.gui.homePageGUI;
import hu.blok1.veilingsite.client.gui.loginGUI;
import hu.blok1.veilingsite.client.gui.personalPageGUI;
import hu.blok1.veilingsite.client.gui.rightMenuGUI;
import hu.blok1.veilingsite.client.gui.searchResults;
import hu.blok1.veilingsite.shared.User;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Veilingsite implements EntryPoint, ValueChangeHandler<String> {
	private HorizontalPanel headerPanel = new HorizontalPanel();
	private HorizontalPanel footerPanel = new HorizontalPanel();
	private VerticalPanel userMenu = new VerticalPanel();
	private VerticalPanel mainPanel = new VerticalPanel();

	private headerGUI header;
	private rightMenuGUI rightMenu;

	private User currentUser = new User();

	public void onModuleLoad() {
		headerPanel.setWidth(100 + "%");
		userMenu.setWidth(100 + "%");
		mainPanel.setWidth(100 + "%");
		footerPanel.setWidth(100 + "%");
		History.addValueChangeHandler(this);

		if (History.getToken().isEmpty()) {
			History.newItem("home");
		} else {
			changePage(History.getToken());
		}

		RootPanel.get("header").add(headerPanel);
		RootPanel.get("main").add(mainPanel);
		RootPanel.get("right").add(userMenu);
		RootPanel.get("footer").add(footerPanel);

		header = new headerGUI(headerPanel, this);
		rightMenu = new rightMenuGUI(this);
		footerGUI footer = new footerGUI(footerPanel);

	}

	public void onValueChange(ValueChangeEvent<String> event) {
		changePage(History.getToken());
	}

	public void changePage(String token) {
		if (History.getToken().equals("home")) {
			homePageGUI homePage = new homePageGUI(mainPanel);

		} else if (History.getToken().startsWith("searchResults")) {
			String[] tokenArray = GetTokenArrayCat(History.getToken());
			searchResults searchResults = new searchResults(mainPanel, this,
					tokenArray[1], tokenArray[2]);

		} else if (History.getToken().equals("login")) {
			loginGUI login = new loginGUI(this);
		} else if (History.getToken().equals("accountaanmaken")) {
			createAccountGUI createAccount = new createAccountGUI(mainPanel,
					this);
		} else if (History.getToken().equals("adminIndex")) {
			adminIndex aIndex = new adminIndex(mainPanel, this);
		} else if (History.getToken().equals("adminUserStatus")) {
			adminUserStatus aIndex = new adminUserStatus(mainPanel, this);
		} else if (History.getToken().equals("adminProductStatus")) {
			adminProductStatus aIndex = new adminProductStatus(mainPanel, this);
		} else if (History.getToken().equals("userCreateProduct")) {
			UserCreateProductGUI aIndex = new UserCreateProductGUI(mainPanel,
					this);
		} else if (History.getToken().equals("userCreateProductList")) {
			CreateProductListGUI aIndex = new CreateProductListGUI(mainPanel,
					this);
			
			
		} else if (History.getToken().startsWith("productSpecifications")) {
			String[] tokenArray = GetTokenArrayProduct(History.getToken());
			ProductSpecificationsGUI aIndex = new ProductSpecificationsGUI(
					mainPanel, this, tokenArray[1]);
		} else if (History.getToken().equals("personalPage")) {
			personalPageGUI personalPage = new personalPageGUI(this);
		} else if (History.getToken().equals("logout")) {
			setUser(new User());
			reloadHeader();
			History.newItem("home");
		}

		else if (History.getToken().equals("easteregg")) {
			mainPanel.clear();
			mainPanel
					.add(new Image(
							"http://1.bp.blogspot.com/-u3FKHnFg8cA/Td56DmfliyI/AAAAAAAAAJ8/fTCFNCTs7iE/s1600/trollface%255B1%255D.jpg"));
		} else {
			mainPanel.clear();
			mainPanel.add(new Label("Unknown Page"));
		}
	}

	public User getUser() {
		return currentUser;
	}

	public void setUser(User user) {
		currentUser = user;
	}

	public void reloadHeader() {
		header.reload();
		reloadRightMenu();
	}

	public void reloadRightMenu() {
		rightMenu.reload();
	}

	public VerticalPanel getMainPanel() {
		return mainPanel;
	}

	public VerticalPanel getRightMenu() {
		return userMenu;
	}

	private String[] GetTokenArrayCat(String token) {
		String[] tokenArray = null;
		if (token.contains("//"))
			tokenArray = token.split("//", 3);

		return tokenArray;
	}
	
	private String[] GetTokenArrayProduct(String token) {
		String[] tokenArray = null;
		if (token.contains("//"))
			tokenArray = token.split("//", 2);

		return tokenArray;
	}
}
